import time
import asyncio


async def run(url):
    print("开始向'%s'要数据……" % (url))
    await asyncio.sleep(2)
    data = "'%s'的数据" % (url)
    return data


def call_back(future):
    print("call_back:", future.result())


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    tasks = []
    t1 = time.time()

    for url in ["百度", "阿里", "腾讯", "新浪"]:
        coroutine = run(url)
        task = asyncio.ensure_future(coroutine)
        task.add_done_callback(call_back)
        tasks.append(task)

    # 同时添加4个异步任务
    # asyncio.wait(tasks) 将任务的列表又变成 <coroutine object wait at 0x7f80f43408c0>
    loop.run_until_complete(asyncio.wait(tasks))

    t2 = time.time()
    print("总耗时：%.2f" % (t2 - t1))
